<?php


namespace app\common\service\order;


use api\channel\JdUnion\openOrderRow;
use api\channel\jutuike\ActivityJutuikeChannle;
use api\channel\jutuike\AroundUnion;
use api\channel\jutuike\Coupon;
use api\channel\jutuike\EleUnion;
use api\channel\jutuike\FoodUnion;
use api\channel\jutuike\TravelUnion;
use api\channel\meituan\alliance\Seckill;
use api\channel\meituan\alliance\SeckillAgent;
use api\channel\meituan\alliance\Takeout;
use api\channel\meituan\alliance\TakeoutAgent;
use api\channel\meituan\alliance\ToShop;
use api\channel\mobile\MobileSelf;
use api\channel\movie\MovieSelf;
use api\channel\taobao\EleOrderChannel;
use app\common\service\order\coupon_order\jd_union\JingDongUnionSyncOrder;
use app\common\service\order\coupon_order\jutuike_channel\ActivityUnion;
use app\common\service\order\coupon_order\jutuike_channel\JutuikeChannelBase;
use app\common\service\order\coupon_order\jutuike_channel\Union;
use app\common\service\order\coupon_order\jutuike_channel\Vip;
use app\common\service\order\coupon_order\meituan\MeituanChannelBase;
use app\common\service\order\coupon_order\meituan\MeituanPubUnionSyncOrder;
use app\common\service\order\coupon_order\meituan_takeout\MeituanTakeoutSyncOrder;
use app\common\service\order\coupon_order\mobile\MobileSyncOrder;
use app\common\service\order\coupon_order\movie\MovieSyncOrder;
use app\common\service\order\coupon_order\taobao\EleTaobao;
use think\Exception;

/**
 * //目前聚推客拉取订单
 * Class CouponSyncOrderService
 * @package app\common\service\order
 */
class CouponSyncOrderService
{
    public $products_serv;

    const PRODUCT_UNION = 'union'; //统一拉取订单

    public function __construct()
    {
        $this->products_serv = $this->product_serv();
    }


    public function syncAllOrder()
    {
        $this->assignments($this->product_serv());

        //代理自己配置的
        $this->assignments($this->incomeTypeInterface());

    }


    /**
     * 分配任务
     * @param $products_serv
     */
    public function assignments($products_serv)
    {
        $this->products_serv = $products_serv;
        //同步公司配置的秘钥订单 CPS
        foreach ($this->products_serv as $serv_type => $item) {
            if ($serv_type == PRODUCT_MEITUAN_TAKEOUT_SERV){ //不走统一拉取定时脚本
                continue;
            }
            try {
                $this->syncOrder($serv_type);
            } catch (\Exception $exception) {
                \think\facade\Log::error($exception->getMessage());
            }
        }
    }


    /**
     * 同步订单
     * @param int $serv_type
     * @throws Exception
     */
    public function syncOrder($serv_type = PRODUCT_TRAVEL_OIL_SERV)
    {


        if (empty($this->products_serv[$serv_type])) throw new Exception('不存在同步配置');

        /**
         * @var MeituanChannelBase $service
         * @var JutuikeChannelBase
         * @var MeituanTakeoutSyncOrder
         */
        $class = $this->products_serv[$serv_type]['saveOrder'];
        $extend_class = $this->products_serv[$serv_type]['extend'];
        $service = new $class;
        $extend = new $extend_class();
        //拉订单
        $service->setExtendApi($extend)->httpGetOrder();

        //接口更新详情，更新数据库已有订单的信息

        if (method_exists($extend, 'getOneOrder')) {
            $service->updateOrderInfoByOne();
        }

    }

    /**
     * 获取自己公司配置的秘钥接口
     * @return array
     */
    public function product_serv()
    {
        $result = [
            PRODUCT_FILM_SERV =>[
                'saveOrder' => MovieSyncOrder::class,//保存
                'extend' => MovieSelf::class //影票表同步数据
            ],
            //话费
//            PRODUCT_MOBILE_SERV =>[
//                'saveOrder' => MobileSyncOrder::class,//保存
//                'extend' => MobileSelf::class //话费自营同步
//            ],
            //权益
            PRODUCT_COUPON_SERV => [
                'saveOrder' => Vip::class,//保存
                'extend' => Coupon::class //第三方接口
            ],
            //出行
            //为什么同一个接口，一样的订单要分多个订单去拉呢？(优化:同一个秘钥同一个page同一个url就加缓存咯，这样也就是拉一次的效果) https://inews.gtimg.com/newsapp_ls/0/14879756870/0.jiketuchuang.png
            PRODUCT_TRAVEL_OIL_SERV => [     //这个注释掉了，统一在统一接口结算了
                'saveOrder' => Union::class,
                'extend' => TravelUnion::class
            ],

            //周边游
            PRODUCT_AROUND_SHOP_SERV => [
                'saveOrder' => Union::class,
                'extend' => AroundUnion::class
            ],

            //餐饮
            PRODUCT_FOOD_SHOP_SERV => [
                'saveOrder' => Union::class,
                'extend' => FoodUnion::class
            ],
            //饿了么
            PRODUCT_ELE_SERV => [
                'saveOrder' => Union::class,
                'extend' => EleUnion::class
            ],


            /**
             * 以上为聚推客
             */

            //美团秒杀
            PRODUCT_SECKILL_SERV => [
                'saveOrder' => MeituanPubUnionSyncOrder::class,
                'extend' => Seckill::class
            ],

            //美团到店
            PRODUCT_METUAN_SERV => [
                'saveOrder' => MeituanPubUnionSyncOrder::class,
                'extend' => ToShop::class
            ],

            //美团外卖
            PRODUCT_MEITUAN_TAKEOUT_SERV => [
                'saveOrder' => MeituanTakeoutSyncOrder::class,
                'extend' => Takeout::class
            ],

            //京东联盟--京喜
            PRODUCT_MOBILE_CPS_SERV => [
                //保存订单
                'saveOrder' => JingDongUnionSyncOrder::class,
                //拉取订单的实现接口
                'extend' => openOrderRow::class,
            ],

            //聚推客统一渠道 以上没有保存的，最后面进行保存---------这个一定要最后一个
            9999999999 => [
                //保存订单
                'saveOrder' => ActivityUnion::class,
                //拉取订单的实现接口
                'extend' => ActivityJutuikeChannle::class,
            ],
        ];
        if (env('ELE_TAOBAO',false)){
            //饿了么--官方
            $result[PRODUCT_ELE_SERV]  = [
                'saveOrder' => EleTaobao::class,
                'extend' => EleOrderChannel::class
            ];
        }

        return  $result;


    }

    /**
     * 判断接口的供应航
     */
    public function incomeTypeInterface()
    {
        return [
            //美团秒杀
            PRODUCT_SECKILL_SERV => [
                'saveOrder' => MeituanPubUnionSyncOrder::class,
                'extend' => SeckillAgent::class
            ],

            //一起拉的这个就不用了，product_serv()方法里为什么要这么写呢？因为需要跟踪到哪个供应商
//            //美团到店
//            PRODUCT_METUAN_SERV => [
//                'saveOrder' => MeituanPubUnionSyncOrder::class,
//                'extend' => ToShopAgent::class
//            ],

            //美团外卖
            PRODUCT_MEITUAN_TAKEOUT_SERV => [
                'saveOrder' => MeituanTakeoutSyncOrder::class,
                'extend' => TakeoutAgent::class
            ],
        ];
    }


}